package cn.itcast.bos.service.base.impl;

import cn.itcast.bos.dao.base.SubAreaRepository;
import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.base.FixedArea;
import cn.itcast.bos.domain.base.SubArea;
import cn.itcast.bos.service.base.AreaService;
import cn.itcast.bos.service.base.FixedAreaService;
import cn.itcast.bos.service.base.SubAreaService;
import cn.itcast.bos.utils.ExcelUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.ArrayList;


@Service
@Transactional
public class SubAreaServiceImpl implements SubAreaService {

    @Autowired
    private AreaService areaService;
    @Autowired
    private FixedAreaService fixedAreaService;

    @Autowired
    private SubAreaRepository subAreaRepository;

    @Override
    public void uploadArea(File file, String fileFileName) {
        //读取Excel文件进行解析
        Sheet sheet = ExcelUtils.getExcelSheetByFile(file, fileFileName);
        ArrayList<SubArea> subAreas = new ArrayList<>();
        System.out.println(fileFileName+"---");
        for (Row row:sheet) {
            if (row.getRowNum()>0){
                String id = row.getCell(0).getStringCellValue();                //分区自身ID！
                String fixedAreaId = row.getCell(1).getStringCellValue();       //定区ID
                String areaId = row.getCell(2).getStringCellValue();            //区域ID
                String keyWords = row.getCell(3).getStringCellValue();          //关键字！
                String startNum = row.getCell(4).getStringCellValue();          //起始号！
                String endNum = row.getCell(5).getStringCellValue();            //终止号！
                //Character
                String value = row.getCell(6).getStringCellValue();
                Character single = value.charAt(0);                                     //单双号！
                String assistKeyWords =row.getCell(7).getStringCellValue();    //辅助关键字！

                SubArea subArea = new SubArea();
                subArea.setId(id);
                subArea.setSingle(single);
                subArea.setStartNum(startNum);
                subArea.setEndNum(endNum);
                subArea.setAssistKeyWords(assistKeyWords);
                subArea.setKeyWords(keyWords);
                //特殊属性
                FixedArea fixedArea = fixedAreaService.findOne(fixedAreaId);
                Area area = areaService.findOne(areaId);

                subArea.setFixedArea(fixedArea);
                subArea.setArea(area);
                subAreas.add(subArea);
            }
            subAreaRepository.save(subAreas);

        }

    }

    @Override
    public Page<SubArea> findPageData(Specification<SubArea> specification, Pageable pageable) {
        Page<SubArea> all = subAreaRepository.findAll(specification, pageable);
        return all;
    }
}
